import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/api/http_server/report.dart';
import 'package:loxen/dialog/toast.dart';
import 'package:loxen/mode/inspiration/inspiration_info_bean.dart';
import 'package:loxen/routes/routes.dart';
import 'package:loxen/utils/config.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:loxen/view/inspiration/poster_widget.dart';
import 'package:sharesdk_plugin/sharesdk_plugin.dart';

class ShareDialog extends StatelessWidget {
  final InspirationInfo info;

  const ShareDialog({Key key, this.info}) : super(key: key);

  void shareToSina(BuildContext context) {
    if (Platform.isIOS){
      SSDKMap params = SSDKMap()
        ..setGeneral(
            null,
            '${info.title} ${Constants.BASE_URL}/share/idea/article/${info.id}',
            [
              info.cover.split('?')[0]
            ],
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            SSDKContentTypes.auto);

      SharesdkPlugin.share(ShareSDKPlatforms.sina, params,
              (SSDKResponseState state, Map userdata, Map contentEntity,
              SSDKError error) {
            if (state == SSDKResponseState.Success) {
              Toast.show('分享成功', context);
            }
          });
    } else {
      SSDKMap params = SSDKMap()
        ..setGeneral(
            info.title,
            info.title,
            [info.cover.split('?')[0],],
            info.cover.split('?')[0],
            null,
            '${Constants.BASE_URL}/share/idea/article/${info.id}',
            null,
            null,
            null,
            null,
            SSDKContentTypes.webpage);

      SharesdkPlugin.share(ShareSDKPlatforms.sina, params,
              (SSDKResponseState state, Map userdata, Map contentEntity,
              SSDKError error) {
            if (state == SSDKResponseState.Success) {
              Toast.show('分享成功', context);
            }
          });
    }
  }

  void shareToWechat(BuildContext context) {
    SSDKMap params = SSDKMap()
      ..setWechat(
          info.brief,
          info.title,
          '${Constants.BASE_URL}/share/idea/article/${info.id}',
          info.cover.split('?')[0],
          null,
          null,
          null,
          info.cover.split('?')[0],
          null,
          null,
          null,
          null,
          null,
          SSDKContentTypes.webpage,
          ShareSDKPlatforms.wechatSession);

    SharesdkPlugin.share(ShareSDKPlatforms.wechatSession, params,
        (SSDKResponseState state, Map userdata, Map contentEntity,
            SSDKError error) {
      if (state == SSDKResponseState.Success) {
        Toast.show('分享成功', context);
      }
    });
  }

  void shareToWechattimeLine(BuildContext context) {
    SSDKMap params = SSDKMap()
      ..setWechat(
          info.brief,
          info.title,
          '${Constants.BASE_URL}/share/idea/article/${info.id}',
          info.cover.split('?')[0],
          null,
          null,
          null,
          info.cover.split('?')[0],
          null,
          null,
          null,
          null,
          null,
          SSDKContentTypes.webpage,
          ShareSDKPlatforms.wechatTimeline);

    SharesdkPlugin.share(ShareSDKPlatforms.wechatTimeline, params,
        (SSDKResponseState state, Map userdata, Map contentEntity,
            SSDKError error) {
      if (state == SSDKResponseState.Success) {
        Toast.show('分享成功', context);
      }
    });
  }

  void shareToQQ(BuildContext context) {
    SSDKMap params = SSDKMap()
      ..setQQ(
          info.brief,
          info.title,
          '${Constants.BASE_URL}/share/idea/article/${info.id}',
          null,
          null,
          null,
          null,
          null,
          info.cover.split('?')[0],
          null,
          null,
          '${Constants.BASE_URL}/share/idea/article/${info.id}',
          null,
          null,
          SSDKContentTypes.webpage,
          ShareSDKPlatforms.qq);
    SharesdkPlugin.share(ShareSDKPlatforms.qq, params, (SSDKResponseState state,
        Map userdata, Map contentEntity, SSDKError error) {
      if (state == SSDKResponseState.Success) {
        Toast.show('分享成功', context);
      }
    });
  }

  void shareToQQTimeLine(BuildContext context) {
    SSDKMap params = SSDKMap()
      ..setQQ(
          info.brief,
          info.title,
          '${Constants.BASE_URL}/share/idea/article/${info.id}',
          null,
          null,
          null,
          null,
          "",
          info.cover.split('?')[0],
          null,
          null,
          '${Constants.BASE_URL}/share/idea/article/${info.id}',
          null,
          null,
          SSDKContentTypes.webpage,
          ShareSDKPlatforms.qZone);
    SharesdkPlugin.share(ShareSDKPlatforms.qZone, params,
        (SSDKResponseState state, Map userdata, Map contentEntity,
            SSDKError error) {
      if (state == SSDKResponseState.Success) {
        Toast.show('分享成功', context);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: 660.w,
        height: 750.h,
        color: Theme.of(context).backgroundColor,
        child: Column(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(top: 40.h),
              child: Text(
                StringResources.fen_xiang,
                style: TextStyle(
                    fontSize: 32.sp,
                    color: Theme.of(context).textTheme.subtitle2.color,
                    decoration: TextDecoration.none,
                    fontWeight: FontWeight.bold),
              ),
            ),
            Padding(
              padding: EdgeInsets.only(top: 80.h, left: 50.w, right: 50.w),
              child: GridView.count(
                shrinkWrap: true,
                crossAxisCount: 4,
                physics: const NeverScrollableScrollPhysics(),
                mainAxisSpacing: 75.h,
                children: <Widget>[
                  itemBuild(context, onClick: () {
                    //TODO 生成海报
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => PosterWidget(
                                  poster: info.share_image,
                                )));
                  },
                      image: 'images/poster.png',
                      imageW: 32.w,
                      imageH: 37.w,
                      tag: '生成海报'),
                  itemBuild(context, onClick: () {
                    shareToSina(context);
                    //TODO 分享微博
                  },
                      image: 'images/share_weibo.png',
                      imageW: 44.w,
                      imageH: 36.w,
                      tag: '新浪微博'),
                  itemBuild(context, onClick: () {
                    shareToWechat(context);
                    //TODO 微信好友
                  },
                      image: 'images/share_weixin.png',
                      imageW: 43.w,
                      imageH: 37.w,
                      tag: '微信好友'),
                  itemBuild(context, onClick: () {
                    shareToWechattimeLine(context);
                    //TODO 朋友圈
                  },
                      image: 'images/wechat_moments.png',
                      imageW: 32.w,
                      imageH: 37.w,
                      tag: '朋友圈'),
                  itemBuild(context, onClick: () {
                    shareToQQ(context);
                    //TODO QQ好友
                  },
                      image: 'images/share_qq.png',
                      imageW: 35.w,
                      imageH: 40.w,
                      tag: 'QQ好友'),
                  itemBuild(context, onClick: () {
                    shareToQQTimeLine(context);
                    //TODO QQ空间
                  },
                      image: 'images/qq_space.png',
                      imageW: 43.w,
                      imageH: 41.w,
                      tag: 'QQ空间'),
                  itemBuild(context, onClick: () {
                    Clipboard.setData(ClipboardData(
                      text:
                          '${Constants.BASE_URL}/share/idea/article/${info.id}',
                    ));
                    Toast.show('已复制到剪贴板', context);
                    //TODO 复制链接
                  },
                      image: 'images/copy_link.png',
                      imageW: 38.w,
                      imageH: 38.w,
                      tag: '复制链接'),
                  itemBuild(context, onClick: () {
                    //TODO 举报
                    Navigator.of(context).pushNamed(Routes.report_page,
                        arguments: {
                          'id': info.id.toString(),
                          'reportType': ReportType.IDEA_ARTICLE
                        });
                  },
                      image: 'images/report.png',
                      imageW: 32.w,
                      imageH: 37.w,
                      tag: '举报'),
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.only(
                  left: 80.w, right: 80.w, top: 80.h, bottom: 39.h),
              child: Divider(),
            ),
            Material(
              color: Theme.of(context).backgroundColor,
              child: InkWell(
                onTap: () {
                  Navigator.of(context).pop(false);
                },
                child: Text(
                  '取消分享',
                  style: TextStyle(fontSize: 28.sp, color: Color(0xFF82868E)),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget itemBuild(BuildContext context,
      {Function() onClick,
      String image,
      String tag,
      double imageW,
      double imageH}) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: Column(
        children: <Widget>[
          Material(
            color: Theme.of(context).backgroundColor,
            child: InkWell(
              borderRadius: BorderRadius.all(Radius.circular(88.w)),
              onTap: onClick,
              child: Container(
                width: 88.w,
                height: 88.w,
                decoration: BoxDecoration(
                  border: Border.all(color: Color(0xFF71747A)),
                  borderRadius: BorderRadius.all(Radius.circular(88.w)),
                ),
                child: Center(
                  child: Image.asset(
                    image,
                    width: imageW,
                    height: imageH,
                  ),
                ),
              ),
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: 15.h),
            child: Text(
              tag,
              style: TextStyle(
                  fontSize: 20.sp,
                  color: Theme.of(context).textTheme.subtitle2.color,
                  decoration: TextDecoration.none),
            ),
          )
        ],
      ),
    );
  }
}
